package com.sg.controller.bff;

import com.sg.service.biz.bff.TaskService;
import com.sg.dto.biz.bff.req.*;
import com.sg.dto.biz.bff.res.*;
import com.sg.common.ro.OkrResult;
import com.sg.dto.integration.component.ExcelHeaderFieldDataDto;
import com.sg.dto.integration.component.ExportDataDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Api(value = "Task", tags = {"Task"})
@RestController
// @RequestMapping("/oms/task/")
public class TaskController {

    @Autowired
    TaskService taskService;

    /**
     * B3-查询汇报给我的任务列表
     * gen by moon at 12/18/2022, 9:03:24 PM
     */
    @PostMapping("/oms/task/queryTaskReportedToMeList")
    @ApiOperation(value = "B3-查询汇报给我的任务列表", response = OkrResult.class)
    public OkrResult<BffQueryTaskReportedToMeListRespDto> queryTaskReportedToMeList(@RequestBody(required = false) @Validated BffQueryTaskReportedToMeListReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskReportedToMeList(reqDTO));
    }

    /**
     * B3-查询我相关的任务列表
     * gen by moon at 12/19/2022, 5:34:41 AM
     */
    @PostMapping("/oms/task/queryTaskRelatedList")
    @ApiOperation(value = "B3-查询我相关的任务列表", response = OkrResult.class)
    public OkrResult<BffQueryTaskRelatedListRespDto> queryTaskRelatedList(@RequestBody(required = false) @Validated BffQueryTaskRelatedListReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskRelatedList(reqDTO));
    }

    /**
     * B3-暂存我汇报的任务
     * gen by moon at 12/20/2022, 2:47:41 AM
     */
    @PostMapping("/oms/task/tempStorageTaskReport")
    @ApiOperation(value = "B3-暂存我汇报的任务", response = OkrResult.class)
    public OkrResult<BffTempStorageTaskReportRespDto> tempStorageTaskReport(@RequestBody(required = false) @Validated BffTempStorageTaskReportReqDto reqDTO) {
        return OkrResult.success(taskService.tempStorageTaskReport(reqDTO));
    }

    /**
     * B3-查询任务评价信息列表
     * gen by moon at 12/17/2022, 5:51:29 AM
     */
    @PostMapping("/oms/task/queryTaskEvaInfoList")
    @ApiOperation(value = "B3-查询任务评价信息列表", response = OkrResult.class)
    public OkrResult<BffQueryTaskEvaInfoListRespDto> queryTaskEvaInfoList(@RequestBody(required = false) @Validated BffQueryTaskEvaInfoListReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskEvaInfoList(reqDTO));
    }

    /**
     * B3-查询我汇报的任务评价信息详情
     * gen by moon at 11/15/2022, 9:46:42 PM
     */
    @PostMapping("/oms/task/queryTaskReportEvaInfoDetail")
    @ApiOperation(value = "B3-查询我汇报的任务评价信息详情", response = OkrResult.class)
    public OkrResult<BffQueryTaskReportEvaInfoDetailRespDto> queryTaskReportEvaInfoDetail(@RequestBody(required = false) @Validated BffQueryTaskReportEvaInfoDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskReportEvaInfoDetail(reqDTO));
    }

    /**
     * B3-查汇报给我的任务评价信息详情
     * gen by moon at 11/17/2022, 5:45:14 PM
     */
    @PostMapping("/oms/task/queryTaskReportedToMeEvaInfoDetail")
    @ApiOperation(value = "B3-查汇报给我的任务评价信息详情", response = OkrResult.class)
    public OkrResult<BffQueryTaskReportedToMeEvaInfoDetailRespDto> queryTaskReportedToMeEvaInfoDetail(@RequestBody(required = false) @Validated BffQueryTaskReportedToMeEvaInfoDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskReportedToMeEvaInfoDetail(reqDTO));
    }

    /**
     * B3-查询我汇报的任务列表
     * gen by moon at 12/18/2022, 9:03:28 PM
     */
    @PostMapping("/oms/task/queryTaskReportList")
    @ApiOperation(value = "B3-查询我汇报的任务列表", response = OkrResult.class)
    public OkrResult<BffQueryTaskReportListRespDto> queryTaskReportList(@RequestBody(required = false) @Validated BffQueryTaskReportListReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskReportList(reqDTO));
    }

    /**
     * B3-撤销汇报我汇报的任务
     * gen by moon at 9/24/2022, 4:16:12 AM
     */
    @PostMapping("/oms/task/cancelApprovedTaskReport")
    @ApiOperation(value = "B3-撤销汇报我汇报的任务", response = OkrResult.class)
    public OkrResult<BffCancelApprovedTaskReportRespDto> cancelApprovedTaskReport(@RequestBody(required = false) @Validated BffCancelApprovedTaskReportReqDto reqDTO) {
        return OkrResult.success(taskService.cancelApprovedTaskReport(reqDTO));
    }

    /**
     * B3-审核通过汇报给我的任务
     * gen by moon at 1/13/2023, 5:55:33 AM
     */
    @PostMapping("/oms/task/approveSucceedTaskReportedToMe")
    @ApiOperation(value = "B3-审核通过汇报给我的任务", response = OkrResult.class)
    public OkrResult<BffApproveSucceedTaskReportedToMeRespDto> approveSucceedTaskReportedToMe(@RequestBody(required = false) @Validated BffApproveSucceedTaskReportedToMeReqDto reqDTO) {
        return OkrResult.success(taskService.approveSucceedTaskReportedToMe(reqDTO));
    }

    /**
     * B3-审核失败汇报给我的任务
     * gen by moon at 12/17/2022, 9:07:41 PM
     */
    @PostMapping("/oms/task/approveFailTaskReportedToMe")
    @ApiOperation(value = "B3-审核失败汇报给我的任务", response = OkrResult.class)
    public OkrResult<BffApproveFailTaskReportedToMeRespDto> approveFailTaskReportedToMe(@RequestBody(required = false) @Validated BffApproveFailTaskReportedToMeReqDto reqDTO) {
        return OkrResult.success(taskService.approveFailTaskReportedToMe(reqDTO));
    }

    /**
     * B3-查询评价模板下汇报相关配置详情
     * gen by moon at 1/10/2023, 4:17:42 AM
     */
    @PostMapping("/oms/task/queryConfigOfReportingTaskDetail")
    @ApiOperation(value = "B3-查询评价模板下汇报相关配置详情", response = OkrResult.class)
    public OkrResult<BffQueryConfigOfReportingTaskDetailRespDto> queryConfigOfReportingTaskDetail(@RequestBody(required = false) @Validated BffQueryConfigOfReportingTaskDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryConfigOfReportingTaskDetail(reqDTO));
    }

    /**
     * B3-查询我汇报的任务标准数据详情
     * gen by moon at 1/4/2023, 8:57:45 PM
     */
    @PostMapping("/oms/task/queryTaskReportStandardDataDetail")
    @ApiOperation(value = "B3-查询我汇报的任务标准数据详情", response = OkrResult.class)
    public OkrResult<BffQueryTaskReportStandardDataDetailRespDto> queryTaskReportStandardDataDetail(@RequestBody(required = false) @Validated BffQueryTaskReportStandardDataDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryTaskReportStandardDataDetail(reqDTO));
    }

    /**
     * B3-生成目标内容汇报任务（人工真实测试）
     * gen by moon at 9/22/2022, 1:09:06 AM
     */
    @PostMapping("/oms/task/addTargetReportTasks")
    @ApiOperation(value = "B3-生成目标内容汇报任务（人工真实测试）", response = OkrResult.class)
    public OkrResult<BffAddTargetReportTasksRespDto> addTargetReportTasks(@RequestBody(required = false) @Validated BffAddTargetReportTasksReqDto reqDTO) {
        return OkrResult.success(taskService.addTargetReportTasks(reqDTO));
    }

    /**
     * B3-计算汇报任务的标准数据（人工真实测试）
     * gen by moon at 9/28/2022, 7:15:21 AM
     */
    @PostMapping("/oms/task/calculateTaskReportStandardData")
    @ApiOperation(value = "B3-计算汇报任务的标准数据（人工真实测试）", response = OkrResult.class)
    public OkrResult<BffCalculateTaskReportStandardDataRespDto> calculateTaskReportStandardData(@RequestBody(required = false) @Validated BffCalculateTaskReportStandardDataReqDto reqDTO) {
        return OkrResult.success(taskService.calculateTaskReportStandardData(reqDTO));
    }

    /**
     * B3-提交我汇报的任务进度值和目标值
     * gen by moon at 1/11/2023, 5:53:01 AM
     */
    @PostMapping("/oms/task/commitTaskReportProgressAndTargetData")
    @ApiOperation(value = "B3-提交我汇报的任务进度值和目标值", response = OkrResult.class)
    public OkrResult<BffCommitTaskReportProgressAndTargetDataRespDto> commitTaskReportProgressAndTargetData(@RequestBody(required = false) @Validated BffCommitTaskReportProgressAndTargetDataReqDto reqDTO) {
        return OkrResult.success(taskService.commitTaskReportProgressAndTargetData(reqDTO));
    }

    /**
     * B3-查询单条汇报任务评价信息详情
     * gen by moon at 12/29/2022, 9:53:08 PM
     */
    @PostMapping("/oms/task/querySingleTaskReportEvaInfoDetail")
    @ApiOperation(value = "B3-查询单条汇报任务评价信息详情", response = OkrResult.class)
    public OkrResult<BffQuerySingleTaskReportEvaInfoDetailRespDto> querySingleTaskReportEvaInfoDetail(@RequestBody(required = false) @Validated BffQuerySingleTaskReportEvaInfoDetailReqDto reqDTO) {
        return OkrResult.success(taskService.querySingleTaskReportEvaInfoDetail(reqDTO));
    }

    /**
     * B3-提交单条汇报任务评价信息
     * gen by moon at 12/16/2022, 9:12:26 PM
     */
    @PostMapping("/oms/task/commitSingleTaskReportEvaInfo")
    @ApiOperation(value = "B3-提交单条汇报任务评价信息", response = OkrResult.class)
    public OkrResult<BffCommitSingleTaskReportEvaInfoRespDto> commitSingleTaskReportEvaInfo(@RequestBody(required = false) @Validated BffCommitSingleTaskReportEvaInfoReqDto reqDTO) {
        return OkrResult.success(taskService.commitSingleTaskReportEvaInfo(reqDTO));
    }

    /**
     * B3-执行汇报任务评价审核元素判断
     * gen by moon at 12/20/2022, 2:47:48 AM
     */
    @PostMapping("/oms/task/implementJudgeTaskReportEvaAndCheck")
    @ApiOperation(value = "B3-执行汇报任务评价审核元素判断", response = OkrResult.class)
    public OkrResult<BffImplementJudgeTaskReportEvaAndCheckRespDto> implementJudgeTaskReportEvaAndCheck(@RequestBody(required = false) @Validated BffImplementJudgeTaskReportEvaAndCheckReqDto reqDTO) {
        return OkrResult.success(taskService.implementJudgeTaskReportEvaAndCheck(reqDTO));
    }

    /**
     * B3-查询下级汇报任务列表
     * gen by moon at 3/30/2023, 7:17:18 PM
     */
    @PostMapping("/oms/task/querySubReportingTaskList")
    @ApiOperation(value = "B3-查询下级汇报任务列表", response = OkrResult.class)
    public OkrResult<BffQuerySubReportingTaskListRespDto> querySubReportingTaskList(@RequestBody(required = false) @Validated BffQuerySubReportingTaskListReqDto reqDTO) {
        return OkrResult.success(taskService.querySubReportingTaskList(reqDTO));
    }

    /**
     * B3-执行补偿生成全网的汇报任务
     * gen by moon at 5/12/2023, 7:21:43 PM
     */
    @PostMapping("/oms/task/implementCompGenerationEvaObjectTask")
    @ApiOperation(value = "B3-执行补偿生成全网的汇报任务", response = OkrResult.class)
    public OkrResult<BffImplementCompGenerationEvaObjectTaskRespDto> implementCompGenerationEvaObjectTask(@RequestBody(required = false) @Validated BffImplementCompGenerationEvaObjectTaskReqDto reqDTO) {
        return OkrResult.success(taskService.implementCompGenerationEvaObjectTask(reqDTO));
    }

    /**
     * B3-执行继续汇报元素分析
     * gen by moon at 9/27/2023, 11:07:09 PM
     */
    @PostMapping("/oms/task/implementCanReportElementAnalysis")
    @ApiOperation(value = "B3-执行继续汇报元素分析", response = OkrResult.class)
    public OkrResult<BffImplementCanReportElementAnalysisRespDto> implementCanReportElementAnalysis(@RequestBody(required = false) @Validated BffImplementCanReportElementAnalysisReqDto reqDTO) {
        return OkrResult.success(taskService.implementCanReportElementAnalysis(reqDTO));
    }

    /**
     * B3-查询待汇报任务标准数据详情
     * gen by moon at 9/28/2023, 3:11:06 PM
     */
    @PostMapping("/oms/task/queryToBeReportedTaskStandardDataDetail")
    @ApiOperation(value = "B3-查询待汇报任务标准数据详情", response = OkrResult.class)
    public OkrResult<BffQueryToBeReportedTaskStandardDataDetailRespDto> queryToBeReportedTaskStandardDataDetail(@RequestBody(required = false) @Validated BffQueryToBeReportedTaskStandardDataDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryToBeReportedTaskStandardDataDetail(reqDTO));
    }

    /**
     * B3-待汇报任务提交标准数据元素分析
     * gen by moon at 9/28/2023, 3:11:14 PM
     */
    @PostMapping("/oms/task/implementReportTaskSubmitDataAnalyse")
    @ApiOperation(value = "B3-待汇报任务提交标准数据元素分析", response = OkrResult.class)
    public OkrResult<BffImplementReportTaskSubmitDataAnalyseRespDto> implementReportTaskSubmitDataAnalyse(@RequestBody(required = false) @Validated BffImplementReportTaskSubmitDataAnalyseReqDto reqDTO) {
        return OkrResult.success(taskService.implementReportTaskSubmitDataAnalyse(reqDTO));
    }

    /**
     * B3-执行汇报任务暂存及提交元素分析
     * gen by moon at 10/1/2023, 12:52:59 AM
     */
    @PostMapping("/oms/task/implementJudgeTaskReportTempAndSubmit")
    @ApiOperation(value = "B3-执行汇报任务暂存及提交元素分析", response = OkrResult.class)
    public OkrResult<BffImplementJudgeTaskReportTempAndSubmitRespDto> implementJudgeTaskReportTempAndSubmit(@RequestBody(required = false) @Validated BffImplementJudgeTaskReportTempAndSubmitReqDto reqDTO) {
        return OkrResult.success(taskService.implementJudgeTaskReportTempAndSubmit(reqDTO));
    }

    /**
     * B3-执行提醒下级去汇报
     * gen by moon at 10/22/2023, 7:32:40 PM
     */
    @PostMapping("/oms/task/implementRemindToReport")
    @ApiOperation(value = "B3-执行提醒下级去汇报", response = OkrResult.class)
    public OkrResult<BffImplementRemindToReportRespDto> implementRemindToReport(@RequestBody(required = false) @Validated BffImplementRemindToReportReqDto reqDTO) {
        return OkrResult.success(taskService.implementRemindToReport(reqDTO));
    }

    /**
     * B3提交待汇报进度值、实际值及预算标准数据
     * gen by moon at 3/16/2024, 3:15:20 AM
     */
    @PostMapping("/oms/task/commitToBeReportedProAndActAndBudData")
    @ApiOperation(value = "B3提交待汇报进度值、实际值及预算标准数据", response = OkrResult.class)
    public OkrResult<BffCommitToBeReportedProAndActAndBudDataRespDto> commitToBeReportedProAndActAndBudData(@RequestBody(required = false) @Validated BffCommitToBeReportedProAndActAndBudDataReqDto reqDTO) {
        return OkrResult.success(taskService.commitToBeReportedProAndActAndBudData(reqDTO));
    }

    /**
     * B4提交评价标准数据
     * gen by moon at 4/25/2024, 2:24:26 AM
     */
    @PostMapping("/oms/task/commitEvaStandardData")
    @ApiOperation(value = "B4提交评价标准数据", response = OkrResult.class)
    public OkrResult<BffCommitEvaStandardDataRespDto> commitEvaStandardData(@RequestBody(required = false) @Validated BffCommitEvaStandardDataReqDto reqDTO) {
        return OkrResult.success(taskService.commitEvaStandardData(reqDTO));
    }

    /**
     * B3设汇报任务问题数据状态
     * gen by moon at 7/13/2024, 1:13:28 AM
     */
    @PostMapping("/oms/task/implementSetReportTaskProblemDataStatus")
    @ApiOperation(value = "B3设汇报任务问题数据状态", response = OkrResult.class)
    public OkrResult<BffImplementSetReportTaskProblemDataStatusRespDto> implementSetReportTaskProblemDataStatus(@RequestBody(required = false) @Validated BffImplementSetReportTaskProblemDataStatusReqDto reqDTO) {
        return OkrResult.success(taskService.implementSetReportTaskProblemDataStatus(reqDTO));
    }

    /**
     * B3查询未发布的汇报资料详情
     * gen by moon at 7/19/2024, 5:46:33 PM
     */
    @PostMapping("/oms/task/queryNotReleaseTaskInfoDetail")
    @ApiOperation(value = "B3查询未发布的汇报资料详情", response = OkrResult.class)
    public OkrResult<BffQueryNotReleaseTaskInfoDetailRespDto> queryNotReleaseTaskInfoDetail(@RequestBody(required = false) @Validated BffQueryNotReleaseTaskInfoDetailReqDto reqDTO) {
        return OkrResult.success(taskService.queryNotReleaseTaskInfoDetail(reqDTO));
    }

    /**
     * B3查询新汇报任务列表
     * gen by moon at 9/4/2024, 8:41:53 PM
     */
    @PostMapping("/oms/task/queryReportTaskList")
    @ApiOperation(value = "B3查询新汇报任务列表", response = OkrResult.class)
    public OkrResult<BffQueryReportTaskListRespDto> queryReportTaskList(@RequestBody(required = false) @Validated BffQueryReportTaskListReqDto reqDTO) {
        return OkrResult.success(taskService.queryReportTaskList(reqDTO));
    }

    private String convertCode(String enCode) {
        if (enCode.equals("EDITING")) {
            return "草稿";
        } else if (enCode.equals("TOBESIGN")) {
            return "草稿";
        } else if (enCode.equals("ONGOING")) {
            return "草稿";
        } else if (enCode.equals("FAILED")) {
            return "草稿";
        } else if (enCode.equals("OVERDUE")) {
            return "草稿";
        } else if (enCode.equals("NOT_AT_THE")) {
            return "草稿";
        } else if (enCode.equals("UNCOMPLETED")) {
            return "草稿";
        } else if (enCode.equals("COMPLETED")) {
            return "草稿";
        } else if (enCode.equals("EVA")) {
            return "草稿";
        } else if (enCode.equals("AUDIT")) {
            return "草稿";
        } else if (enCode.equals("SUCCESS")) {
            return "已成功";
        } else if (enCode.equals("EXPIRED")) {
            return "已逾期";
        } else if (enCode.equals("UNREPORTED")) {
            return "待汇报";
        } else if (enCode.equals("UNLIMITED")) {
            return "不限";
        } else if (enCode.equals("ENDED")) {
            return "已结束";
        }
        return "未知";
    }

    /**
     * B3执行导出汇报任务明细
     * gen by moon at 12/31/2024, 5:35:00 PM
     */
    @PostMapping("/oms/task/implementReportTaskExportDetails")
    @ApiOperation(value = "B3执行导出汇报任务明细", response = OkrResult.class)
    public void implementReportTaskExportDetails(@RequestBody(required = false) @Validated BffImplementReportTaskExportDetailsReqDto reqDTO, HttpServletResponse response) {
        taskService.implementReportTaskExportDetails(reqDTO, (excelData) -> {
            if (excelData.containsKey("exportFileFormat")) {
                String exportFileFormat = (String) excelData.get("exportFileFormat");
                String exportExcelFileName = (String) excelData.get("exportExcelFileName");
                List<ExcelHeaderFieldDataDto> headerDataList = (List<ExcelHeaderFieldDataDto>) excelData.get("excelHeaderFieldDataList");
                List<ExportDataDto> dataList = (List<ExportDataDto>) excelData.get("exportDataList");

                String fileFullNameTmp = exportExcelFileName + "." + exportFileFormat;
                String fileFullName = null;
                try {
                    fileFullName = URLEncoder.encode(fileFullNameTmp, "UTF-8").replaceAll("\\+", "%20");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileFullName);
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                Workbook workbook = null;
                try {
                    workbook = new XSSFWorkbook();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                // 创建一个工作表sheet
                Sheet sheet = workbook.createSheet("Sheet1");

                // 创建表头行
                Row headerRow = sheet.createRow(0);
                int column = 0;
                Cell numCell = headerRow.createCell(column++);
                numCell.setCellValue("编号");
                for (ExcelHeaderFieldDataDto field : headerDataList) {
                    Cell cell = headerRow.createCell(column++);
                    cell.setCellValue(field.getExcelHeaderCnFields());
                }

                // 填充数据
                long rowCount = 1l;
                for (int i = 0; i < dataList.size(); i++) {
                    Row row = sheet.createRow(i + 1);
                    ExportDataDto rowData = dataList.get(i);
                    for (int j = 0; j < 11; j++) {
                        Cell cell = row.createCell(j);
                        if (j == 0) {
                            cell.setCellValue(rowCount);
                        } else if (j == 1 && rowData.getExportDataFields1() != null) {
                            cell.setCellValue(rowData.getExportDataFields1());
                        } else if (j == 2 && rowData.getExportDataFields2() != null) {
                            cell.setCellValue(rowData.getExportDataFields2());
                        } else if (j == 3 && rowData.getExportDataFields3() != null) {
                            cell.setCellValue(rowData.getExportDataFields3());
                        } else if (j == 4 && rowData.getExportDataFields4() != null) {
                            cell.setCellValue(convertCode(rowData.getExportDataFields4()));
                        } else if (j == 5 && rowData.getExportDataFields5() != null) {
                            cell.setCellValue(rowData.getExportDataFields5());
                        } else if (j == 6 && rowData.getExportDataFields6() != null) {
                            cell.setCellValue(rowData.getExportDataFields6());
                        } else if (j == 7 && rowData.getExportDataFields7() != null) {
                            cell.setCellValue(rowData.getExportDataFields7());
                        } else if (j == 8 && rowData.getExportDataFields8() != null) {
                            cell.setCellValue(rowData.getExportDataFields8());
                        } else if (j == 9 && rowData.getExportDataFields9() != null) {
                            cell.setCellValue(rowData.getExportDataFields9());
                        } else if (j == 10 && rowData.getExportDataFields10() != null) {
                            cell.setCellValue(rowData.getExportDataFields10());
                        }
                    }
                    rowCount++;
                }

                // 自动调整列宽
                for (int i = 0; i < headerDataList.size(); i++) {
                    sheet.autoSizeColumn(i);
                }

                // 将工作簿写入文件
                try {
                    ServletOutputStream outputStream = response.getOutputStream();
                    workbook.write(outputStream);
                    workbook.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        workbook.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        return;
    }


    /**
     * B3执行导出汇报任务明细
     * gen by moon at 12/31/2024, 5:35:00 PM
     */
    @GetMapping("/oms/task/implementReportTaskExportDetailsGet")
    @ApiOperation(value = "B3执行导出汇报任务明细", response = OkrResult.class)
    public void implementReportTaskExportDetailsGet(@RequestParam("cycleId") String cycleId,
                                                    @RequestParam("parentSubMidCycleType") String parentSubMidCycleType,
                                                    @RequestParam("evaluationTemplateId") String evaluationTemplateId,
                                                    HttpServletResponse response) {
        BffImplementReportTaskExportDetailsReqDto reqDTO = new BffImplementReportTaskExportDetailsReqDto();
        reqDTO.setCycleId(cycleId);
        reqDTO.setParentSubMidCycleType(parentSubMidCycleType);
        reqDTO.setEvaluationTemplateId(evaluationTemplateId);
        taskService.implementReportTaskExportDetails(reqDTO, (excelData) -> {
            if (excelData.containsKey("exportFileFormat")) {
                String exportFileFormat = (String) excelData.get("exportFileFormat");
                String exportExcelFileName = (String) excelData.get("exportExcelFileName");
                List<ExcelHeaderFieldDataDto> headerDataList = (List<ExcelHeaderFieldDataDto>) excelData.get("excelHeaderFieldDataList");
                List<ExportDataDto> dataList = (List<ExportDataDto>) excelData.get("exportDataList");

                String fileFullNameTmp = exportExcelFileName + "." + exportFileFormat;
                String fileFullName = null;
                try {
                    fileFullName = URLEncoder.encode(fileFullNameTmp, "UTF-8").replaceAll("\\+", "%20");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileFullName);
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                Workbook workbook = null;
                try {
                    workbook = new XSSFWorkbook();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                // 创建一个工作表sheet
                Sheet sheet = workbook.createSheet("Sheet1");

                // 创建表头行
                Row headerRow = sheet.createRow(0);
                int column = 0;
                Cell numCell = headerRow.createCell(column++);
                numCell.setCellValue("编号");
                for (ExcelHeaderFieldDataDto field : headerDataList) {
                    Cell cell = headerRow.createCell(column++);
                    cell.setCellValue(field.getExcelHeaderCnFields());
                }

                // 填充数据
                long rowCount = 1l;
                for (int i = 0; i < dataList.size(); i++) {
                    Row row = sheet.createRow(i + 1);
                    ExportDataDto rowData = dataList.get(i);
                    for (int j = 0; j < 11; j++) {
                        Cell cell = row.createCell(j);
                        if (j == 0) {
                            cell.setCellValue(rowCount);
                        } else if (j == 1 && rowData.getExportDataFields1() != null) {
                            cell.setCellValue(rowData.getExportDataFields1());
                        } else if (j == 2 && rowData.getExportDataFields2() != null) {
                            cell.setCellValue(rowData.getExportDataFields2());
                        } else if (j == 3 && rowData.getExportDataFields3() != null) {
                            cell.setCellValue(rowData.getExportDataFields3());
                        } else if (j == 4 && rowData.getExportDataFields4() != null) {
                            cell.setCellValue(convertCode(rowData.getExportDataFields4()));
                        } else if (j == 5 && rowData.getExportDataFields5() != null) {
                            cell.setCellValue(rowData.getExportDataFields5());
                        } else if (j == 6 && rowData.getExportDataFields6() != null) {
                            cell.setCellValue(rowData.getExportDataFields6());
                        } else if (j == 7 && rowData.getExportDataFields7() != null) {
                            cell.setCellValue(rowData.getExportDataFields7());
                        } else if (j == 8 && rowData.getExportDataFields8() != null) {
                            cell.setCellValue(rowData.getExportDataFields8());
                        } else if (j == 9 && rowData.getExportDataFields9() != null) {
                            cell.setCellValue(rowData.getExportDataFields9());
                        } else if (j == 10 && rowData.getExportDataFields10() != null) {
                            cell.setCellValue(rowData.getExportDataFields10());
                        }
                    }
                    rowCount++;
                }

                // 自动调整列宽
                for (int i = 0; i < headerDataList.size(); i++) {
                    sheet.autoSizeColumn(i);
                }

                // 将工作簿写入文件
                try {
                    ServletOutputStream outputStream = response.getOutputStream();
                    workbook.write(outputStream);
                    workbook.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        workbook.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        return;
    }
}
